package com.hanthink.web.controller.pc;

import com.hanthink.common.annotation.Log;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.common.utils.poi.ExcelUtil;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.EmQualityWeighing;
import com.hanthink.system.domain.EmXflh;
import com.hanthink.system.domain.label.LabelVerification;
import com.hanthink.system.service.IEmXflhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 质量称重Controller
 * 
 * @author tzy
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/system/xflh")
public class EmXflhController extends BaseController
{
    @Autowired
    private IEmXflhService emXflhService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询质量称重列表
     */
    @PreAuthorize("@ss.hasPermi('system:xflh:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmXflh emQualityWeighing)
    {
        startPage();
        List<EmXflh> list = emXflhService.selectEmQualityWeighingList(emQualityWeighing);
        return getDataTable(list);
    }

    /**
     * 导出质量称重列表
     */
//    @PreAuthorize("@ss.hasPermi('system:weighing:export')")
    @Log(title = "修过留痕", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:export')")
    @GetMapping("/export")
    public AjaxResult export(EmXflh emQualityWeighing)
    {
        List<EmXflh> list = emXflhService.selectEmQualityWeighingList(emQualityWeighing);
        ExcelUtil<EmXflh> util = new ExcelUtil<EmXflh>(EmXflh.class);
        return util.exportExcel(list, "weighing");
    }

    /**
     * 获取质量称重详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:weighing:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(emXflhService.selectEmQualityWeighingById(id));
    }


    @GetMapping("/getOne/{equipKey}")
    public AjaxResult getOne(@PathVariable("equipKey")String key){
        EmXflh emQualityWeighing = emXflhService.selectOneByEquipKey(key);
        return AjaxResult.success(emQualityWeighing);
    }

    /**
     * 新增质量称重
     */
//    @PreAuthorize("@ss.hasPermi('system:weighing:add')")
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:add')")
    @Log(title = "修过留痕", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmXflh emQualityWeighing)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String username = loginUser.getUsername();
        emQualityWeighing.setCreatedBy(username);
        return toAjax(emXflhService.insertEmQualityWeighing(emQualityWeighing));
    }

    /**
     * 修改质量称重
     */
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:edit')")
    @Log(title = "修过留痕", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmXflh emQualityWeighing)
    {
        return toAjax(emXflhService.updateEmQualityWeighing(emQualityWeighing));
    }

    /**
     * 删除质量称重
     */
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:remove')")
    @Log(title = "修过留痕", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(emXflhService.deleteEmQualityWeighingByIds(ids));
    }

    /**
     * 导入数据
     */
    @Log(title = "修过留痕", businessType = BusinessType.IMPORT)

    @PostMapping("/importData")
    @PreAuthorize("@ss.hasPermi('label:LabelVerification:import')")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EmXflh> util = new ExcelUtil(EmXflh.class);
        List<EmXflh> labelList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = emXflhService.importPlan(labelList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<EmXflh> util = new ExcelUtil(EmXflh.class);
        return util.importTemplateExcel("修复留痕数据");
    }
}
